package com.zzptc.zzsgz.bdsjws.Adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.github.lzyzsd.randomcolor.RandomColor;
import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.views.CircleTextImageView;
import com.zzptc.zzsgz.bdsjws.vo.Contact;
import com.zzptc.zzsgz.bdsjws.vo.MobileInfo;

import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;


public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.MyHoldView>{
    private List<Contact> list;
//    private List<Contact> soslist = MyApplication.getSosList;
    int count;
    TextView textView;
    public ContactAdapter(List<Contact> list , TextView textView) {
        this.list = list;
        this.textView = textView;
    }

    @Override
    public MyHoldView onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.show_contact_item,parent,false);
        MyHoldView myViewHolder = new MyHoldView(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyHoldView holder, int position) {
        //在绑定之前需要数据
        Contact contact = list.get(position);
        holder.ctiv.setText(contact.getName().substring(1));
        RandomColor randomColor = new RandomColor();
        int color = randomColor.randomColor();
        holder.ctiv.setFillColor(color);
        holder.tv_username.setText(contact.getName());
        holder.tv_tel.setText(contact.getPhonenum());
        holder.tv_addr.setText(contact.getAddress());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHoldView extends RecyclerView.ViewHolder implements View.OnClickListener{
        @BindView(R.id.ctiv)
        CircleTextImageView ctiv;
        @BindView(R.id.tv_name)
        TextView tv_username;
        @BindView(R.id.tv_tel)
        TextView tv_tel;
        @BindView(R.id.tv_addr)
        TextView tv_addr;
        @BindView(R.id.cb_chooes)
        CheckBox cb_chooes;

        public MyHoldView(View itemView){
            super(itemView);
            ButterKnife.bind(this , itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            Contact c = list.get(position);
            String tel = c.getPhonenum().replace(" ","").replace("-","");
//          Toast.makeText(MyApplication.getContext(), "+++"+c.getName()+"+++", Toast.LENGTH_SHORT).show();
            if (tel != null&&!"".equals(tel)&&tel.matches("^1[3578]\\d{9}$")){
                if (cb_chooes.isChecked()){
                    cb_chooes.setChecked(false);
                    count--;
                }else{
                    if(count < 3){
                        cb_chooes.setChecked(true);
//                        count++;

                    }else {
                        Toast.makeText(MyApplication.getContext(), "不能超过三个人", Toast.LENGTH_SHORT).show();
                    }
                }
            }else {
                    Toast.makeText(MyApplication.getContext(), "请选择手机号码", Toast.LENGTH_SHORT).show();
                }

        }
    }

}
